<?php
/**
 * Fastlib核心文件
 *
 * $Id: fastlib.php 130 2010-10-10 16:47:37Z Administrator $
 * $Revision: 130 $
 * $Author: Administrator $
 * $Date: 2010-10-10 16:47:37 +0000 (Sun, 10 Oct 2010) $
 * 初始化fastlib环境，加载类库
 * 
 * @author glzaboy<glzaboy@163.com>
 * @package fastlib
 * @copyright 2005-2008 (c) glzaboy
 */
date_default_timezone_set ( 'UTC' );
$flibcfg = null;
if (version_compare ( PHP_VERSION, '5.1.2', '<' )) {
	die ( 'Fastlib require php5.1.2 or later. current version is ' . PHP_VERSION );
}
/**
 * Fastlib目录路径
 *
 */
define ( "__FASTLIB__", strtr ( dirname ( __FILE__ ), DIRECTORY_SEPARATOR, '/' ) . "/" );
/**
 * fastlib系统目录
 *
 */
define ( "__FASTSYSTEM__", __FASTLIB__ . "system/" );
/**
 * Fastlib 版本信息
 *
 */
define ( "__FASTVER__", '1.4 test $Revision: 130 $' );
unset ( $GLOBALS, $HTTP_GET_VARS, $HTTP_POST_VARS, $HTTP_COOKIE_VARS, $HTTP_SERVER_VARS, $HTTP_ENV_VARS, $HTTP_POST_FILES, $HTTP_SESSION_VARS );
if (ini_get ( 'register_globals' )) {
	foreach ( array_merge ( array_keys ( $_POST ), array_keys ( $_GET ), array_keys ( $_REQUEST ) ) as $v ) {
		if (in_array ( $v, array ('_GET', '_POST', '__FILES', '__COOKIE', '__SESSION', '_SERVER', '_ENV', '_REQUEST' ) )) {
			continue;
		}
		unset ( $$v );
	}
}
require 'fastlib.cfg.php';
ini_set ( 'include_path', __FASTLIB__ . 'vendor' . PATH_SEPARATOR . ini_get ( 'include_path' ) );
ini_set ( 'arg_separator.output', '&amp;' );
ini_set ( 'arg_separator.intput', '&' );
/**
 * 系统文件装载器
 * 
 * @author glzaboy<glzaboy@163.com>
 *
 */
class FastLoad {
	/**
	 * 载入Fastlib系统类
	 *
	 * @param String $class 类名
	 * @return bool 如果成功返回true
	 */
	static public function loadFastClass($class) {
		global $flibcfg;
		$class = trim ( $class );
		$classes = array ();
		array_push ( $classes, 'data' );
		array_push ( $classes, 'fasttpl' );
		array_push ( $classes, 'fastupload' );
		array_push ( $classes, 'gdplus' );
		array_push ( $classes, 'http' );
		array_push ( $classes, 'ip' );
		array_push ( $classes, 'log' );
		array_push ( $classes, 'message' );
		array_push ( $classes, 'mvc' );
		array_push ( $classes, 'page' );
		array_push ( $classes, 'rss' );
		array_push ( $classes, 'url' );
		if (class_exists ( 'log' )) {
			$log = new log ( $flibcfg ['logTimeFormat'], $flibcfg ['logFileName'] );
		}
		if (class_exists ( $class )) {
			if (isset ( $log ) && $log) {
				$log->putlog ( "The class {$class} has been exists,But you script want load it." );
			}
			return false;
		}
		if (! (is_string ( $class ) && preg_match ( "/^[a-zA-Z0-9]+$/", $class ))) {
			if (isset ( $log ) && $log) {
				$log->putlog ( "The class {$class} looks not like a class ." );
			}
			return false;
		}
		if (in_array ( $class, $classes )) {
			FastLoad::fastinclude ( 'class/' . trim ( $class ) . '.php', 'require' );
		} else {
			return false;
		}
	}
	/**
	 * 载入站点自定义类
	 *
	 * @param String $classlib 类名
	 * @return bool 如果成功返回true
	 */
	static public function loadClass($class) {
		global $flibcfg;
		$class = trim ( $class );
		if (class_exists ( 'log' )) {
			$log = new log ( $flibcfg ['logTimeFormat'], $flibcfg ['logFileName'] );
		}
		if (class_exists ( $class )) {
			if (isset ( $log ) && $log) {
				$log->putlog ( "The class {$class} has been exists,But you script want load it." );
			}
			return false;
		}
		if (! (is_string ( $class ) && preg_match ( "/^[a-zA-Z0-9]+$/", $class ))) {
			if (isset ( $log ) && $log) {
				$log->putlog ( "The class {$class} looks not like a class ." );
			}
			return false;
		}
		if (defined ( '__SITE__' ) && file_exists ( getSiteCurrent ( true ) . "lib/class/{$class}.php" )) {
			if (isset ( $log ) && $log) {
				$log->putlog ( "Loading sitelib class {$class}..." );
			}
			FastLoad::fastinclude ( 'lib/class/' . trim ( $class ) . '.php', 'require' );
			return true;
		} else {
			if (isset ( $log ) && $log) {
				$log->putlog ( "The class {$class} file doesn't exists,But you script wanted load it" );
			}
			return false;
		}
	}
	/**
	 * 载入系统函数集
	 *
	 * @param String $funclib 涵数集名称
	 * @param Int $file 当前文件行，用于调试
	 * @param Int $line 当前文件行,用于调试
	 * @return bool 如果成功返回true
	 */
	public function loadLib($funclib, $file = __FILE__, $line = __LINE__) {
		global $flibcfg;
		if (class_exists ( "log" )) {
			$log = new log ( $flibcfg ['logTimeFormat'], $flibcfg ['logFileName'] );
		}
		if (! (is_string ( $funclib ) && preg_match ( "/^[a-z0-9]+$/", $funclib ))) {
			if (isset ( $log ) && $log) {
				$log->putlog ( "The class {$funclib} looks not like a functions .", $file, $line );
			}
			return false;
		}
		if (file_exists ( __FASTSYSTEM__ . 'functions/' . $funclib . '.functions.php' )) {
			if (isset ( $log ) && $log) {
				$log->putlog ( "Loading fastlib functions {$funclib}...", $file, $line );
			}
			FastLoad::fastinclude ( 'functions/' . trim ( $funclib ) . '.functions.php', 'require_once' );
			return true;
		} elseif (defined ( '__SITE__' ) && file_exists ( getSiteCurrent ( true ) . 'lib/functions/' . $funclib . '.functions.php' )) { //search sitelib
			if (isset ( $log ) && $log) {
				$log->putlog ( "Loading site functions {$funclib}...", $file, $line );
			}
			FastLoad::fastinclude ( 'lib/functions/' . trim ( $funclib ) . '.functions.php', 'require_once' );
			return true;
		} else {
			if (isset ( $log ) && $log) {
				$log->putlog ( "The functions {$funclib} file doesn't exists,But you script wanted load it", $file, $line );
			}
			return false;
		}
	}
	/**
	 * 包含一个文件
	 * 
	 * 统一处理包含文件
	 *
	 * 本数据统一处理包含文件，主要方式
	 * include,include_once,require,require_once
	 * 分别等同include,include_once,require,require_once语句
	 * 
	 * @param string $file 文件名
	 * @param 包含方式  $type
	 * @return bool 包含结果
	 */
	public function fastinclude($file, $type = 'include') {
		if (file_exists ( __FASTSYSTEM__ . $file )) {
			$file = __FASTSYSTEM__ . $file;
		} elseif (defined ( '__SITE__' ) && file_exists ( getSiteCurrent ( true ) . $file )) {
			$file = getSiteCurrent ( true ) . $file;
		}
		switch (strtolower ( $type )) {
			case 'include' :
				return include $file;
				break;
			case 'include_once' :
				return include_once $file;
				break;
			case 'require' :
				return require $file;
				break;
			case 'require_once' :
				return require_once $file;
				break;
			default :
				return false;
		}
	}
}
spl_autoload_register ( array ('FastLoad', 'LoadFastClass' ) );//自动装入fastlib类
spl_autoload_register ( array ('FastLoad', 'LoadClass' ) );//普通类
require (__FASTSYSTEM__ . './functions/core.php');
require (__FASTSYSTEM__ . './misc/functions.php');
FastLoad::loadLib ( "set", __FILE__, __LINE__ );
FastLoad::loadLib ( "timezone", __FILE__, __LINE__ );
setHeader ( 'X-Powered-By', 'fastlib' . __FASTVER__ );
global $sitecfg;
if (is_array ( $sitecfg ['sitelist'] )) {
	foreach ( $sitecfg ['sitelist'] as $k => $v ) {
		if (in_array ( $flibcfg ['HOST'], $v )) {
			if (is_dir ( $k )) {
				/**
				 * 多站点时工作站点路径
				 * 
				 */
				define ( '__SITECURRENT__', $k );
			} else {
				setHeader ( 'http', '404 File not found' );
				exit ();
			}
			break;
		}
	}
}
if (! defined ( '__SITECURRENT__' )) {
	define ( '__SITECURRENT__', 'default' );
}
unset ( $sitecfg ['sitelist'] );
if (file_exists ( getSiteCurrent ( true ) . 'setting.php' )) {
	require getSiteCurrent ( true ) . 'setting.php';
}
if (file_exists ( getSiteCurrent ( true ) . 'data/site.setting.php' )) {
	require getSiteCurrent ( true ) . 'data/site.setting.php';
}
if (defined ( '__SITECURRENT__' )) {
	chdir ( __SITECURRENT__ );
}
if (is_array ( $sitecfg )) {
	foreach ( $sitecfg as $k => $v ) {
		$flibcfg [$k] = $v;
	}
}
foreach ( $flibcfg as $k => $v ) {
	setSitecfg ( $k );
}
fixcookie ();
if (strtolower ( $flibcfg ['QUERY_STRING'] ) === 'fastinfo') {
	if (function_exists ( 'fastinfo' )) {
		fastinfo ();
	}
	exit ( 0 );
}
if (substr ( strtolower ( $flibcfg ['QUERY_STRING'] ), 0, 16 ) === substr ( md5 ( 'fast' . date ( 'Y-m-d' ) ), 8, 16 )) {
	fastinfo ( substr ( strtolower ( $flibcfg ['QUERY_STRING'] ), 16, 16 ) );
	exit ( 0 );
}
/**
 * @var bool 调试模式就否打开
 */
define ( "__DEBUG__", ($flibcfg ['debug'] ? true : false) );
if (__DEBUG__) { //调试
	error_reporting ( E_ALL ^ E_NOTICE );
} else { //运行
	error_reporting ( E_ALL );
}
//数据库表映射
$__TABLE = array ();
//CSS
$__CSS = array ();
//JS
$__JS = array ();
//RSS
$__RSS = array ();
//FETCH
$__FETCH = array ();
//Include
$__INCLUDE = array ();
//备份的会话
$__SESSION = null;